package com.fir.my.batis.plus.until;

import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Table;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @Author: tianxubo
 * @CreateTime: 2022-10-18 16:05:40
 * @Description: Jackcess
 */
@Slf4j
public class AccessJackcessUtils {

    public static void main(String[] args) {

        String filePath = "D:\\main.mdb";
        //先创建整个库对应的对象集合 maps[key：表名 value :key表数据集合]； Map<String,Object> 表示一行数据
        List<Map<String, Object>> gPdata = readFileAccessTable(filePath, "GPdata");
        System.out.println(gPdata.toString());

    }

    /**
     * 读取mdb全部表格和数据
     *
     * @param filePath 文件路径
     */
    public static Map<String, List<Map<String, Object>>> readFileAccessTables(String filePath) {
        //Map<String,List<Map<String,Object>>> key：表名 value :数据集合
        Map<String, List<Map<String, Object>>> maps = new HashMap<>();
        File file = new File(filePath);
        if (!file.exists()) {
            throw new RuntimeException("mdb文件不存在[" + filePath + "]");
        }
        try {
            Database db = DatabaseBuilder.open(new File(filePath));
            if (null == db) {
                throw new RuntimeException("无法读取mdb文件[" + filePath + "]");
            }
            Set<String> tableSet = db.getTableNames();
            if (tableSet.size() < 1) {
                throw new RuntimeException("[" + filePath + "]库中不存在表格");
            }
            Charset charSet = db.getCharset();
            if (null == charSet) {
                throw new RuntimeException("[" + filePath + "]库中字符集编码不存在");
            }
            log.debug("字符集：" + charSet.name());
            for (String tableName : tableSet) {
                Table table = db.getTable(tableName);
                List<Map<String, Object>> tableData = new ArrayList<>();
                for (Map<String, Object> rowData : table) {
                    tableData.add(rowData);
                }
                log.debug("表名：" + tableName + ",数据" + tableData.size() + "行");
                maps.put(tableName, tableData);
            }
            db.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return maps;
    }

    /**
     * 读取mdb库中某表数据
     *
     * @param filePath 文件路径
     * @param tableName 表名
     */
    public static List<Map<String, Object>> readFileAccessTable(String filePath, String tableName) {
        //表数据集合
        List<Map<String, Object>> result = new ArrayList<>();
        File file = new File(filePath);

        try {
            if (!file.exists()) {
                throw new RuntimeException("mdb文件不存在[" + filePath + "]");
            }
            Database db = DatabaseBuilder.open(new File(filePath));
            if (null == db) {
                throw new RuntimeException("无法读取mdb文件[" + filePath + "]");
            }
            Set<String> tableSet = db.getTableNames();
            if (tableSet.size() < 1) {
                throw new RuntimeException("[" + filePath + "]库中不存在表格");
            }
            Charset charSet = db.getCharset();
            if (null == charSet) {
                throw new RuntimeException("[" + filePath + "]库中字符集编码不存在");
            }
            log.debug("字符集：" + charSet.name());

            Table table = db.getTable(tableName);
            for (Map<String, Object> rowData : table) {
                result.add(rowData);
            }
            log.debug("表名：" + tableName + ",数据" + result.size() + "行");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

